//
//  XZLDatePickerView.m
//  XZLife
//
//  Created by liangshangjia on 15/12/22.
//  Copyright (c) 2015年 Sami-industry. All rights reserved.
//

#import "XZLDatePickerView.h"
@implementation XZLDatePickerView
+ (instancetype)customPickerView
{
    XZLDatePickerView *pickerView = [[XZLDatePickerView alloc]init];
    [pickerView customInit];
    return pickerView;
}

- (void)customInit
{
    self.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_HEIGHT);
    
    [self addSubview:self.maskView];
    [self addSubview:self.containView];
    [self.containView addSubview:self.titleLabel];
    [self.containView addSubview:self.datePicker];
    [self.containView addSubview:self.cancelButton];
    [self.containView addSubview:self.confirmButton];
    
//    [self hide:DBL_EPSILON];
    [self makeConstraint];
}

- (void)makeConstraint
{
    //    CGFloat height = width * 1.5;
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(100)));
        make.width.equalTo(@((SM_SCREEN_WIDTH - SM_SIZE(20*2))));
    }];
    
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.titleLabel.mas_bottom);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-SM_SIZE(20.f)));
        make.left.equalTo(@(SM_SIZE(20.f)));
        make.width.equalTo(self.confirmButton.mas_width);
        make.height.equalTo(@(SM_SIZE(80)));
        make.top.equalTo(self.datePicker.mas_bottom).offset(SM_SIZE(10));
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cancelButton.mas_right).offset(SM_SIZE(30.f));
        make.right.equalTo(@(-SM_SIZE(20.f)));
        make.bottom.equalTo(self.cancelButton.mas_bottom);
        make.height.equalTo(self.cancelButton.mas_height);
        make.top.equalTo(self.cancelButton.mas_top);
    }];
    
    UIView *separator = [UIView new];
    separator.backgroundColor = SM_placeholdColor;
    [self.containView addSubview:separator];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.confirmButton.mas_centerY);
        make.height.equalTo(self.confirmButton.mas_height);
        make.width.equalTo(@(SM_SIZE(2)));
    }];
}

- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    self.maskView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.0];
    self.containView.transform = CGAffineTransformMakeScale(.01, .01);
    self.maskView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.0];
    [UIView animateWithDuration:.2f animations:^{
        self.maskView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.5f];
        self.containView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide:(CGFloat)time
{
    [UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        self.maskView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.0];
        self.containView.transform = CGAffineTransformMakeScale(.02, .02);
        self.containView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}



#pragma mark - Propertys

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = SM_thumeColor;
        _titleLabel.textColor = SM_whiteColor   ;
        _titleLabel.font = SM_CFont(35);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"选择日期";
    }
    return _titleLabel;
}

- (UIImageView *)maskView
{
    if (!_maskView) {
        _maskView = [[UIImageView alloc]init];
        _maskView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.5f];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        _maskView.userInteractionEnabled = YES;
        [_maskView addGestureRecognizer:tap  ];
    }
    return _maskView;
}

- (UIView *)containView
{
    if (!_containView) {
        _containView = [[UIView alloc]init];
        _containView.backgroundColor = [SM_whiteColor colorWithAlphaComponent:.95];
        _containView.layer.masksToBounds = YES;
        _containView.layer.cornerRadius = SM_SIZE(10.f);
    }
    return _containView;
}

- (UIDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        _datePicker.minimumDate = [NSDate date];
        [_datePicker setValue:SM_grayColor forKey:@"textColor"];
    }
    return _datePicker;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:SM_blackColor forState:UIControlStateNormal];
        [_cancelButton setTitleColor:SM_darkGrayColor forState:UIControlStateHighlighted];
        [_cancelButton.titleLabel setFont:SM_CFont(32)];
        [_cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _cancelButton;
}

- (UIButton *)confirmButton
{
    if (!_confirmButton) {
        _confirmButton = [[UIButton alloc]init];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:SM_thumeColor forState:UIControlStateNormal];
        [_confirmButton setTitleColor:SM_darkGrayColor forState:UIControlStateHighlighted];
        [_confirmButton.titleLabel setFont:SM_CFont(32)];
        [_confirmButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _confirmButton;
}


#pragma mark - Setters
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
}

#pragma mark - Actions
- (void)buttonAction:(UIButton *)sender
{
    if (sender == _cancelButton) {
        [self hide:.2];
        return;
    }else {
        BOOL isSet = sender;
        !self.onclick ?: !isSet ?: self.onclick(self.datePicker.date);
        [self hide:.2];
//        [UIView animateWithDuration:.1f animations:^{
////            sender.transform = CGAffineTransformMakeScale(.9, .9);
//        } completion:^(BOOL finished) {
////            sender.transform = CGAffineTransformIdentity;
//
//        }];
    }
    
}

- (void)tapAction
{
    [self buttonAction:nil];
}
@end
